package com.kool.leitu.service;

import com.kool.leitu.base.bean.vo.PageResult;
import com.kool.leitu.base.bean.vo.PaginationVo;
import com.kool.leitu.base.exception.AppException;
import com.kool.leitu.bean.SyErrorInfoBean;
import com.kool.leitu.dao.SyErrorInfoDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR LUYU
 */
@Deprecated
@Slf4j
@Service
public class ErrorCodeService {

    @Autowired
    private SyErrorInfoDao syErrorInfoDao;

    /**
     * @DESCRIBE 增加
     */
    public void add(SyErrorInfoBean syErrorInfo) {
        if (null == syErrorInfo) {
            throw new AppException(1000001);// 输入信息为空
        }
        SyErrorInfoBean errorInfo = syErrorInfoDao.selectByPK(syErrorInfo);
        if (errorInfo != null) {
            throw new AppException(1000001);// 错误码已经存在
        }

        log.debug(syErrorInfo.toString());
        syErrorInfoDao.insert(syErrorInfo);

        // 刷新缓存 TODO
//		KCache.refresh(CoreConstants.KCACHE_ERRCOD);
    }

    /**
     * @DESCRIBE 修改
     */
    public void update(SyErrorInfoBean syErrorInfo) {
        if (null == syErrorInfo) {
            throw new AppException(1000001);// 输入信息为空
        }
        log.debug(syErrorInfo.toString());
        syErrorInfoDao.update(syErrorInfo);

        // 刷新缓存 TODO
//		KCache.refresh(CoreConstants.KCACHE_ERRCOD);
    }

    /**
     * @DESCRIBE 删除
     */
    public void delete(SyErrorInfoBean syErrorInfo) {
        if (null == syErrorInfo) {
            throw new AppException(1000001);// 输入信息为空
        }
        log.debug(syErrorInfo.toString());
        syErrorInfoDao.delete(syErrorInfo);

        // 刷新缓存  TODO
//		KCache.refresh(CoreConstants.KCACHE_ERRCOD);
    }

    /**
     * @DESCRIBE 分页查询
     */
    public PageResult queryPage(SyErrorInfoBean syErrorInfo, PaginationVo pageInfo) {
        log.debug(syErrorInfo.toString());
        int total = syErrorInfoDao.selectTotal(syErrorInfo);

        int currentPage = pageInfo.getCurrentPage();
        int pageSize = pageInfo.getPageSize();
        int offset = (currentPage - 1) * pageSize;
        int pageCount = (total + pageSize - 1) / pageSize;
        List<SyErrorInfoBean> list = syErrorInfoDao.selectPage(syErrorInfo, offset, pageInfo.getPageSize());

        PageResult pageResult = new PageResult();
        pageResult.setListBusinessValue(new ArrayList<>(list));
        PaginationVo paginationVo = new PaginationVo();
        paginationVo.setCurrentPage(currentPage);
        paginationVo.setPageCount(pageCount);
        paginationVo.setPageSize(pageSize);
        paginationVo.setTotal(total);
        return pageResult;
    }

    /**
     * @DESCRIBE 查询
     */
    public List<SyErrorInfoBean> query(SyErrorInfoBean syErrorInfo) {
        if (null == syErrorInfo) {
            syErrorInfo = new SyErrorInfoBean();
        }

        log.debug(syErrorInfo.toString());
        return syErrorInfoDao.select(syErrorInfo);
    }
}
